package com.example.earthed.config;

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Data
@Configuration
@PropertySource("classpath:application.yml")
public class MinioConfig {

    private static final Logger logger = LoggerFactory.getLogger(MinioConfig.class);

    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;

    @Bean
    public MinioClient minioClient() {

        logger.info("Minio endpoint: {}", endpoint);
        logger.info("Minio accessKey: {}", accessKey);
        logger.info("Minio secretKey: {}", secretKey);

        try {
            return MinioClient.builder()
                   .endpoint(endpoint)
                   .credentials(accessKey, secretKey)
                   .build();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}